# common targets and definitions for debian packaging (simple deb packaging and pypi2deb)

# "simplified" deb package files follow s simple filename pattern (e.g. "python3-foo.deb") without
# any version numbers or architectures. They are useful for build artifact uploads.
DIR_DEBIAN_SIMPLIFIED_PACKAGE_FILES ?= $(DIR_DEBIAN_BUILD)/export
# "wildcard" resolves the home directory
DEB_DPUT_CONFIG ?= $(wildcard ~/.dput.cf)
LINTIAN_ARGS ?=


.PHONY: help
help: help-deb-common

.PHONY: help-deb-common
help-deb-common:
	@echo "deb-related targets common for all packaging methods:"
	@echo "    check-dput-config"
	@echo "    clean-deb-export"
	@echo "    dist-deb-packages-directory"
	@echo "    init-deb   - prepare a deb package skeleton directory (by using 'dh_make')"
	@echo "    test-deb   - run tests for debian packaging ('lintian')"
	@echo

.PHONY: check-dput-config
check-dput-config:
	@[ -e "$(DEB_DPUT_CONFIG)" ] || { \
		echo "Failed to find 'dput.cf' file ($(DEB_DPUT_CONFIG))."; \
		echo "You should provide this file or override its location (DEB_DPUT_CONFIG)."; \
		exit 1; } >&2

.PHONY: init-deb
init-deb:
	@if [ -d debian ]; then \
		echo >&2 "ERROR: cowardly refusing to run 'dh_make' while 'debian/' exists"; \
		exit 1; fi
	dh_make --copyright gpl3 --createorig \
		--packagename "$(notdir $(shell pwd))_$(call get_current_version)"
	@echo "An example 'debian/' directory was created. You should verify its content."
	@echo "Afterwards you may want to remove all debian/*.ex files (examples)."
	@echo "Read the Debian New Maintainer's Guide for more details:"
	@echo "    https://www.debian.org/doc/manuals/maint-guide"

.PHONY: deb-lintian
deb-lintian:
	@if [ -d "$(DIR_DEBIAN_BUILD)" ]; then \
		find "$(DIR_DEBIAN_BUILD)" -type f -name "*.changes"; fi \
			| xargs --max-args=1 --no-run-if-empty lintian $(LINTIAN_ARGS)

.PHONY: test-deb
test-deb: deb-lintian

.PHONY: test
test: test-deb

.PHONY: clean-deb-export
clean-deb-export:
	$(RM) -r "$(DIR_DEBIAN_SIMPLIFIED_PACKAGE_FILES)"

.PHONY: clean
clean: clean-deb-export
